<?php
include 'config.php';

// Brisanje naloga
if(isset($_POST['obrisi_id'])) {
    $id = intval($_POST['obrisi_id']);
    $sql = "DELETE FROM nalog WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $id);
    $stmt->execute();
}

// Priprema filtera
$filter_od = isset($_GET['od']) ? $_GET['od'] : null;
$filter_do = isset($_GET['do']) ? $_GET['do'] : null;
$filter_godina = isset($_GET['godina']) ? intval($_GET['godina']) : null;
$filter_mjesto = isset($_GET['mjesto']) ? $_GET['mjesto'] : null;

// Dohvat jedinstvenih godina za filter
$sql_godine = "SELECT DISTINCT godina_naloga AS godina FROM nalog ORDER BY godina DESC";
$result_godine = $conn->query($sql_godine);

// Dohvat jedinstvenih mjesta (mjesto_izvodjena_radova) za filter
$sql_mjesto = "SELECT DISTINCT mjesto_izvodjena_radova FROM nalog ORDER BY mjesto_izvodjena_radova ASC";
$result_mjesto = $conn->query($sql_mjesto);

// Izgradnja SQL upita s filterima
$where = [];
$params = [];
$types = '';

if (!empty($filter_od)) {
    $where[] = "DATE(vrijeme_pocetka) >= ?";
    $params[] = $filter_od;
    $types .= 's';
}

if (!empty($filter_do)) {
    $where[] = "DATE(vrijeme_pocetka) <= ?";
    $params[] = $filter_do;
    $types .= 's';
}

if ($filter_godina > 0) {
    $where[] = "godina_naloga = ?";
    $params[] = $filter_godina;
    $types .= 'i';
}

if (!empty($filter_mjesto)) {
    $where[] = "mjesto_izvodjena_radova LIKE ?";
    $params[] = "%" . $filter_mjesto . "%";
    $types .= 's';
}

$sql = "SELECT * FROM nalog";
if (!empty($where)) {
    $sql .= " WHERE " . implode(" AND ", $where);
}
$sql .= " ORDER BY godina_naloga DESC, broj_naloga ASC";

// Izvršavanje upita
if (!empty($params)) {
    $stmt = $conn->prepare($sql);
    $stmt->bind_param($types, ...$params);
    $stmt->execute();
    $result = $stmt->get_result();
} else {
    $result = $conn->query($sql);
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Lista Naloga</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        th {
            background-color: #f8f9fa;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        .akcije {
            white-space: nowrap;
        }
        button {
            padding: 6px 12px;
            margin: 2px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .izmijeni {
            background-color: #4CAF50;
            color: white;
        }
        .obrisi {
            background-color: #f44336;
            color: white;
        }
        .stampa {
            background-color: #008CBA;
            color: white;
        }
        .filter-form {
            margin: 20px 0;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 8px;
        }
        .filter-form label {
            margin-right: 5px;
        }
        .filter-form input[type="date"],
        .filter-form select,
        .filter-form button {
            padding: 8px;
            margin-right: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        @media print {
            .filter-form, .back-link, .akcije, button {
                display: none !important;
            }
            table {
                width: 100%;
                font-size: 12px;
            }
            th, td {
                padding: 6px;
            }
        }
    </style>
</head>
<div class="back-link" style="text-align: center; margin: 40px 0;">
    <a href="index.php" style="display: inline-block; padding: 15px 30px; background: #27ae60; color: white; text-decoration: none; border-radius: 8px; box-shadow: 0 3px 6px rgba(0,0,0,0.16);">
        ← Vratite se na početnu
    </a>
</div>
<body>
    <h2>Lista svih naloga</h2>
    
    <!-- Filter forma -->
    <form method="GET" class="filter-form">
        <label for="od">Datum od:</label>
        <input type="date" name="od" id="od" value="<?= isset($_GET['od']) ? $_GET['od'] : '' ?>">
        
        <label for="do">Datum do:</label>
        <input type="date" name="do" id="do" value="<?= isset($_GET['do']) ? $_GET['do'] : '' ?>">
        
        <select name="godina">
            <option value="">Sve godine</option>
            <?php while($row = $result_godine->fetch_assoc()): ?>
                <option value="<?= $row['godina'] ?>" <?= ($row['godina'] == $filter_godina) ? 'selected' : '' ?>>
                    <?= $row['godina'] ?>
                </option>
            <?php endwhile; ?>
        </select>
        
        <select name="mjesto">
            <option value="">Sva mjesta</option>
            <?php while($row_mjesto = $result_mjesto->fetch_assoc()): ?>
                <option value="<?= htmlspecialchars($row_mjesto['mjesto_izvodjena_radova']) ?>" <?= (isset($filter_mjesto) && $filter_mjesto == $row_mjesto['mjesto_izvodjena_radova']) ? 'selected' : '' ?>>
                    <?= htmlspecialchars($row_mjesto['mjesto_izvodjena_radova']) ?>
                </option>
            <?php endwhile; ?>
        </select>
        
        <button type="submit">Filtriraj</button>
        <a href="lista_naloga.php" style="padding: 8px 12px; background: #ddd; border-radius: 4px; text-decoration: none;">Reset</a>
        <button type="button" onclick="window.print()" style="background: #666; color: white;">Print Preview</button>
    </form>

    <?php if($result->num_rows > 0): ?>
    <table>
        <thead>
            <tr>
                <th>Broj naloga</th>
                <th>Godina</th>
                <th>Mjesto radova</th>
                <th>Početak radova</th>
                <th>Završetak radova</th>
                <th>Akcije</th>
            </tr>
        </thead>
        <tbody>
            <?php while($row = $result->fetch_assoc()): ?>
            <tr>
                <td><?= htmlspecialchars($row['broj_naloga']) ?></td>
                <td><?= htmlspecialchars($row['godina_naloga']) ?></td>
                <td><?= htmlspecialchars($row['mjesto_izvodjena_radova']) ?></td>
                <td><?= date('d.m.Y H:i', strtotime($row['vrijeme_pocetka'])) ?></td>
                <td><?= date('d.m.Y H:i', strtotime($row['zavrsetak_radova'])) ?></td>
                <td class="akcije">
                    <a href="izmjeni_nalog.php?id=<?= $row['id'] ?>" class="izmijeni">Izmijeni</a>
                    
                    <form action="lista_naloga.php" method="POST" style="display: inline;">
                        <input type="hidden" name="obrisi_id" value="<?= $row['id'] ?>">
                        <button type="submit" class="obrisi" onclick="return confirm('Jeste li sigurni?')">Obriši</button>
                    </form>
                    
                    <a href="print_nalog.php?id=<?= $row['id'] ?>" target="_blank" class="stampa">Štampa</a>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
    <?php else: ?>
    <p>Nema pronađenih naloga.</p>
    <?php endif; ?>

    <script>
        function confirmDelete() {
            return confirm('Da li ste sigurni da želite obrisati nalog?');
        }
    </script>
</body>
</html>
<?php $conn->close(); ?>
